package com.kgis.map {
    import com.kgis.map.common.enum.MapActionEnum;
    import com.kgis.map.cursor.HandCursor;
    import com.kgis.map.geometry.basetype.Extent;
    import com.kgis.map.geometry.basetype.Size;

    import flash.display.DisplayObject;
    import flash.geom.Rectangle;

    import mx.core.UIComponent;
    import mx.events.ResizeEvent;
    import com.kgis.map.Map;

    /**
     * Map控件类
     * 在mxml里直接可以使用
     */
    public class MapControl extends UIComponent {
        public var map:Map;
        public var numZoomLevels:Number;
        private var _handCursor:HandCursor;
        //  public var serverUrl:String;
        private var _creationHeight:Number = NaN;
        private var _creationWidth:Number = NaN;

        public function MapControl() {
            super();
        }

        /**
         *创建子对象
         */
        override protected function createChildren():void {
            super.createChildren();
            map = new Map();

        /*   var maskComponent:Sprite=new Sprite();
           maskComponent.name = "maskComponent";
           maskComponent.cacheAsBitmap = true;

           maskComponent.graphics.beginFill(0xFF00FF,0.5);
           maskComponent.graphics.drawRect(0,0,this.width,this.height);
           maskComponent.graphics.endFill();
           this.rawChildren.addChild(maskComponent);

         map.mask = maskComponent; */

             //手形鼠标
        }

        public function initMap():void {

            //pkMap.symbolDic = this._symbolDic;
            //pkMap.maxResolution = 518.70703125;
            map.numZoomLevels = numZoomLevels;
            map.maxResolution = maxResolution;

            var extent:Extent = new Extent(maxExtent[0], maxExtent[1], maxExtent[2], maxExtent[3]);
            map.maxExtent = extent;

//            if (serverUrl) {
//                map.serverUrl = serverUrl;
//            }
//            else {
//                serverUrl = "/web/messagebroker/amf";
//                map.serverUrl = serverUrl
//            }

            this.addChild(map);

            if (this.width > 0 && this.height > 0) {
                this.map.size = new Size(this.width, this.height);
            }

            this.map.scrollRect = new Rectangle(0, 0, this.map.size.w, this.map.size.h);

            this.addEventListener(ResizeEvent.RESIZE, this.onResize);



            showHandCursor();
        }

        //设置鼠标光标
        private function showHandCursor():void {
            if (this.map.operateMode == MapActionEnum.MAP_PAN) {
                if (_handCursor == null) {
                    //替换为手形
                    _handCursor = new HandCursor(map)
                    this.addChild(_handCursor);
                }
            }
        }

        /**
         * 更新显示对象
         */ /*		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
           {
           super.updateDisplayList(unscaledWidth, unscaledHeight);

           if (map && (map.width != unscaledWidth || map.height != unscaledHeight))
           {
           map.size=new Size(unscaledWidth, unscaledHeight);
           }
         }*/

        private var _maxResolution:Number;
        private var _maxResolutionChange:Boolean = false;

        public function get maxResolution():Number {
            return _maxResolution;
        }

        public function set maxResolution(value:Number):void {
            if (_maxResolution != value) {
                _maxResolution = value;
                _maxResolutionChange = true;
                invalidateProperties();
            }
        }

        private var _maxExtent:Array;
        private var _maxExtentChange:Boolean = false;

        public function get maxExtent():Array {
            return _maxExtent;
        }

        public function set maxExtent(value:Array):void {
            if (_maxExtent != value) {
                _maxExtent = value;
                _maxExtentChange = true;
                invalidateProperties();
            }
        }

        /**
         * Resize event callback
         */
        private function onResize(event:ResizeEvent):void {
            var o:DisplayObject = event.target as DisplayObject;
            this.map.size = new Size(o.width, o.height);
        }

        /**
         * Width MXML setter
         */
        override public function set width(value:Number):void {
            super.width = value;
            if (map != null)
                this.map.width = value;
            else
                this._creationWidth = value;
        }

        /**
         * Height MXML setter
         */
        override public function set height(value:Number):void {
            super.height = value;
            if (map != null)
                this.map.height = value;
            else
                this._creationHeight = value;
        }

    }
}